﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using System.Globalization;

namespace blackhouse.BlackApart.Business.Payments {
    public class DaPaymentType : IPaymentType {

        #region Fields

        private IReservationDataAccess iReservationData;
        private ImpPaymentType instance;

        #endregion

        #region Properties

        public int Id {
            get { return this.instance.Id; }
            private set { this.instance.Id = value; }
        }

        public string Key {
            get { return this.instance.Key; }
            set { this.instance.Key = value; }
        }

        public string Name {
            get { return this.instance.Name; }
            set { this.instance.Name = value; }
        }

        public bool WaitForCash {
            get { return this.instance.WaitForCash; }
            set { this.instance.WaitForCash = value; }
        }

        public string Description {
            get { return this.instance.Description; }
            set { this.instance.Description = value; }
        }

        public double ExtraRabat {
            get { return this.instance.ExtraRabat; }
            set { this.instance.ExtraRabat = value; }
        }

        #endregion

        #region Constructors

        public DaPaymentType(IReservationDataAccess dataAccess) {
            this.iReservationData = dataAccess;
            this.instance = new ImpPaymentType();
        }

        public DaPaymentType(IReservationDataAccess dataAccess, int paymentTypeId) {
            this.iReservationData = dataAccess;
            this.instance = this.GetInstance(paymentTypeId);
            if (this.instance == null)
                this.instance = new ImpPaymentType();
        }

        public DaPaymentType(IReservationDataAccess dataAccess, PaymentData data) {
            this.iReservationData = dataAccess;
            this.instance = this.ReadInstance(data);
            if (this.instance == null)
                this.instance = new ImpPaymentType();
        }

        #endregion

        #region Methods

        private ImpPaymentType GetInstance(int paymentTypeId) {
            PaymentData data = this.iReservationData.GetPaymentType(paymentTypeId, CultureInfo.CurrentCulture.Name);
            if (data.PaymentTypeId == paymentTypeId)
                return this.ReadInstance(data);
            else return null;

        }

        private ImpPaymentType ReadInstance(PaymentData data) {
            if (data.PaymentTypeId == 0) return null;
            ImpPaymentType payment = new ImpPaymentType();
            payment.Id = data.PaymentTypeId;
            payment.Key = data.Key;
            payment.WaitForCash = data.WaitForCash;
            payment.Name = data.Name;
            payment.Description = data.Description;
            payment.ExtraRabat = data.ExtraRabat;
            return payment;
        }

        public IPaymentType[] GetWebPayments() {
            if (this.instance.WebPayments == null)
                this.instance.WebPayments = this.iReservationData.GetWebPaymentTypes(CultureInfo.CurrentCulture.Name).Select(d => new DaPaymentType(this.iReservationData, d)).ToArray();
            return this.instance.GetWebPayments();
        }

        #endregion

    }
}
